//
// File: hamming.cpp
//
// MATLAB Coder version            : 5.4
// C/C++ source code generated on  : 23-Apr-2025 16:26:18
//

// Include Files
#include "hamming.h"
#include "freqDomainHRV_rtwutil.h"
#include "rt_nonfinite.h"
#include "coder_array.h"
#include <cmath>
#include <string.h>

// Function Definitions
//
// Arguments    : double varargin_1
//                ::coder::array<double, 1U> &w
// Return Type  : void
//
namespace coder {
void hamming(double varargin_1, ::coder::array<double, 1U> &w)
{
  array<double, 2U> y;
  array<double, 1U> b_w;
  double L;
  int i;
  int nx;
  int trivialwin;
  signed char w_data;
  nx = 1;
  w_data = 0;
  trivialwin = 0;
  if (varargin_1 == std::floor(varargin_1)) {
    L = varargin_1;
  } else {
    L = std::round(varargin_1);
  }
  if (L == 0.0) {
    nx = 0;
    trivialwin = 1;
  } else if (L == 1.0) {
    w_data = 1;
    trivialwin = 1;
  }
  w.set_size(nx);
  for (i = 0; i < nx; i++) {
    w[0] = w_data;
  }
  if (trivialwin == 0) {
    double r;
    if (std::isnan(L) || std::isinf(L)) {
      r = rtNaN;
    } else if (L == 0.0) {
      r = 0.0;
    } else {
      r = std::fmod(L, 2.0);
      if (r == 0.0) {
        r = 0.0;
      } else if (L < 0.0) {
        r += 2.0;
      }
    }
    if (r == 0.0) {
      int i1;
      int loop_ub;
      r = L / 2.0;
      if (std::isnan(L / 2.0 - 1.0)) {
        y.set_size(1, 1);
        y[0] = rtNaN;
      } else if (r - 1.0 < 0.0) {
        y.set_size(1, 0);
      } else {
        y.set_size(1, static_cast<int>(r - 1.0) + 1);
        nx = static_cast<int>(r - 1.0);
        for (i = 0; i <= nx; i++) {
          y[i] = i;
        }
      }
      w.set_size(y.size(1));
      nx = y.size(1);
      for (i = 0; i < nx; i++) {
        w[i] = 6.2831853071795862 * (y[i] / (L - 1.0));
      }
      nx = w.size(0);
      for (trivialwin = 0; trivialwin < nx; trivialwin++) {
        w[trivialwin] = std::cos(w[trivialwin]);
      }
      nx = w.size(0);
      for (i = 0; i < nx; i++) {
        w[i] = 0.54 - 0.46 * w[i];
      }
      if (w.size(0) < 1) {
        i = 0;
        trivialwin = 1;
        i1 = -1;
      } else {
        i = w.size(0) - 1;
        trivialwin = -1;
        i1 = 0;
      }
      nx = div_s32(i1 - i, trivialwin);
      b_w.set_size((w.size(0) + nx) + 1);
      loop_ub = w.size(0);
      for (i1 = 0; i1 < loop_ub; i1++) {
        b_w[i1] = w[i1];
      }
      for (i1 = 0; i1 <= nx; i1++) {
        b_w[i1 + w.size(0)] = w[i + trivialwin * i1];
      }
      w.set_size(b_w.size(0));
      nx = b_w.size(0);
      for (i = 0; i < nx; i++) {
        w[i] = b_w[i];
      }
    } else {
      int i1;
      int loop_ub;
      r = (L + 1.0) / 2.0;
      if (std::isnan((L + 1.0) / 2.0 - 1.0)) {
        y.set_size(1, 1);
        y[0] = rtNaN;
      } else if (r - 1.0 < 0.0) {
        y.set_size(1, 0);
      } else {
        y.set_size(1, static_cast<int>(r - 1.0) + 1);
        nx = static_cast<int>(r - 1.0);
        for (i = 0; i <= nx; i++) {
          y[i] = i;
        }
      }
      w.set_size(y.size(1));
      nx = y.size(1);
      for (i = 0; i < nx; i++) {
        w[i] = 6.2831853071795862 * (y[i] / (L - 1.0));
      }
      nx = w.size(0);
      for (trivialwin = 0; trivialwin < nx; trivialwin++) {
        w[trivialwin] = std::cos(w[trivialwin]);
      }
      nx = w.size(0);
      for (i = 0; i < nx; i++) {
        w[i] = 0.54 - 0.46 * w[i];
      }
      if (w.size(0) - 1 < 1) {
        i = 0;
        trivialwin = 1;
        i1 = -1;
      } else {
        i = w.size(0) - 2;
        trivialwin = -1;
        i1 = 0;
      }
      nx = div_s32(i1 - i, trivialwin);
      b_w.set_size((w.size(0) + nx) + 1);
      loop_ub = w.size(0);
      for (i1 = 0; i1 < loop_ub; i1++) {
        b_w[i1] = w[i1];
      }
      for (i1 = 0; i1 <= nx; i1++) {
        b_w[i1 + w.size(0)] = w[i + trivialwin * i1];
      }
      w.set_size(b_w.size(0));
      nx = b_w.size(0);
      for (i = 0; i < nx; i++) {
        w[i] = b_w[i];
      }
    }
  }
}

} // namespace coder

//
// File trailer for hamming.cpp
//
// [EOF]
//
